package com.sumainfor.pc.market.controller;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ListMapMergeUtil;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.feign.BaseFeignZuul;
import com.sumainfor.pc.market.service.OwnerService;
import com.sumainfor.pc.market.service.ProfitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 * 盈利预测
 * @Auther: zhlu
 * @Date: 2018/11/24 17:52
 * @Description:
 */
@RestController
@RequestMapping("/pro")
public class ProfitContrller {

    Logger log= LoggerFactory.getLogger(ProfitContrller.class);

    @Autowired
    BaseFeignZuul BaseFeignZuul;

    @Autowired
    ProfitService profitService;

    @RequestMapping(value = "/AddProfit",method = RequestMethod.POST)
    public JsonResult AddProfit(@RequestBody Map<String,Object> params){
        JsonResult result=new JsonResult();
        if(profitService.AddProfit(params)){
            result.putSuccess("创建盈利预测表成功！");
        }else{
            result.putFailed("服务器繁忙，请稍后重试！");
        }
        return result;
    }

    /**
     * 功能描述:
     * 根据类型获取盈利预测列表。ProState 状态   1草稿  2待审核
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/2 14:15
     */
    @ResponseBody
    @PostMapping("/ProfitList")
    public JsonResult ProfitList(@RequestBody Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        pager.setPageno(Integer.parseInt(params.get("pageno").toString()));
        pager.setPagesize(Integer.parseInt(params.get("pagesize").toString()));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        params.put("delfg",0);
        params.put("ProState",params.get("ProState"));
        int ProfitListCount=profitService.ProfitListCount(params);
        if(ProfitListCount==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,ProfitListCount);
        }
        List<Map<String,Object>>ProfitList=profitService.ProfitList(params);
        result=pageUtils.getJsonResult(ProfitList, params, ProfitListCount);
        return result;
    }

    /**
     * 功能描述:
     * 根据编号获取盈利预测表
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/3 15:04
     */
    @GetMapping("/getProfitMap")
    public JsonResult getProfitMap(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"ProId"})){
            result.putFailed("ProId不能为空！");
            return  result;
        }
        params.put("delfg",0);
        Map<String,Object>getProfitMap=profitService.getProfitMap(params);
        List<Map<String,Object>>getDeptList=BaseFeignZuul.getDeptList(params);
        if(FieldCheckUtil.isMapFieldNotNull(getProfitMap,new String[]{"MiId"})){
            getProfitMap=ListMapMergeUtil.MapTranStringUtils(getProfitMap,getDeptList,"MiId","DeptId","DeptName","MiName");
        }
        result.put(getProfitMap);
        return result;
    }

    /**
     * 功能描述:
     * 修改盈利预测表状态   状态   1草稿  2待审核  3驳回
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/6 10:41
     */
    @ResponseBody
    @RequestMapping(value = "/updateProfit",method = RequestMethod.POST)
    public JsonResult updateProfit(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(profitService.updateProfit(params)){
            result.putSuccess(params.get("hint").toString());
        }else{
            result.putFailed("服务器繁忙，请稍后重试！");
        }
        return result;
    }

    /**
     * 功能描述:
     * 根据类型获取盈利预测列表。ProState 状态   1草稿  2待审核
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/2 14:15
     */
    @ResponseBody
    @GetMapping("/getProfitHouList")
    public JsonResult getProfitHouList(@RequestParam Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        params.put("delfg",0);
        params.put("ProState",params.get("ProState"));
        int getProfitHouListCount=profitService.getProfitHouListCount(params);
        if(getProfitHouListCount==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,0);
        }
        List<Map<String,Object>>getProfitHouList=profitService.getProfitHouList(params);

        if(getProfitHouList.size()>0){
            List<Map<String,Object>>getPostDeptList=BaseFeignZuul.getPostDeptList(new HashMap<>());
            //合并
            getProfitHouList= ListMapMergeUtil.ListMergeUtils(getProfitHouList,getPostDeptList,"DeptId");
        }

//        List<Map<String,Object>>getDeptList=BaseFeignZuul.getDeptList(params);
//
////        getProfitHouList= ListMapMergeUtil.ListTranStringUtils(getProfitHouList,getDeptList,"MiId","DeptId","DeptName","MiName");
//
//        for(Map<String,Object> map:getProfitHouList){
//            if(FieldCheckUtil.isMapFieldNotNull(map,new String[]{"MiId"})){
//                map=ListMapMergeUtil.MapTranStringUtils(map,getDeptList,"MiId","DeptId","DeptName","MiName");
//            }
//        }

        result=pageUtils.getJsonResult(getProfitHouList, params, getProfitHouListCount);
        return result;
    }


    /**
     * 楼号
     * @author zhoucong
     * @data 2019/8/21
     * @Param
     * @return
     */
    @ResponseBody
    @GetMapping("/getOwBuilDing")
    public JsonResult getOwBuilDing(@RequestParam Map<String,Object> params){
        JsonResult result = new JsonResult();
        return result.put(profitService.getOwBuilDing(params));
    }


    /**
     * 室号
     * @author zhoucong
     * @data 2019/8/21
     * @Param
     * @return
     */
    @ResponseBody
    @GetMapping("/getOwRoom")
    public JsonResult getOwRoom(@RequestParam Map<String,Object> params){
        JsonResult result = new JsonResult();
        return result.put(profitService.getOwRoom(params));
    }



    /**
     * 功能描述: 
     * 获取当前项目下的所有楼号，分组
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/25 14:10
     */
    @GetMapping("/getHousingProBuilDingList")
    public JsonResult getHousingProBuilDingList(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        return result.put(profitService.getHousingProBuilDingList(params));
    }

    /**
     * 功能描述: 
     * 获取当前项目楼号下的室号，分组
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/25 14:11
     */
    @GetMapping("/getHousingProRoomList")
    public JsonResult getHousingProRoomList(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"BuilDing"})){
            result.putFailed("BuilDing不能为空！");
            return  result;
        }
        return result.put(profitService.getHousingProRoomList(params));
    }

    /**
     * 功能描述:
     * 获取当前项目-楼号-室号下的区域，分组
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/25 14:11
     */
    @GetMapping("/getHousingConList")
    public JsonResult getHousingConList(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"BuilDing"})){
            result.putFailed("BuilDing不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"HouId"})){
            result.putFailed("HouId不能为空！");
            return  result;
        }
        return result.put(profitService.getHousingConList(params));
    }

    /**
     * 功能描述: 
     * 根据盈利预测编号获取当前盈利预测的审批历史
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/29 14:55
     */
    @ResponseBody
    @RequestMapping(value = "/getProfitCheckList",method=RequestMethod.GET)
    public JsonResult getProfitCheckList(@RequestParam Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        int count=profitService.getProfitCheckListCount(params);
        if(count==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,0);
        }
        List<Map<String,Object>>getProfitCheckList=profitService.getProfitCheckList(params);
        result=pageUtils.getJsonResult(getProfitCheckList, params, count);
        return result;
    }

    /**
     * 功能描述:
     * 根据项目编号集合获取盈利预测所有楼号
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/20 18:57
     */
    @PostMapping("/getComIdByProBuiList")
    public JsonResult getComIdByProBuiList(@RequestBody Map<String,Object>params){
        if(FieldCheckUtil.isMapFieldNotNull(params,new String[]{"comList"})){
            List<String>comList=(List<String>)params.get("comList");
            if(comList.size()==0){
                return JsonResult.error("comList不能为空！");
            }
        }
        return JsonResult.success(profitService.getComIdByProBuiList(params));
    }

    /**
     * 功能描述:
     * 根据项目编号集合和楼号集合获取盈利预测所有室号
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/20 18:57
     */
    @PostMapping("/getComIdAndBuiProRoomList")
    public JsonResult getComIdAndBuiProRoomList(@RequestBody Map<String,Object>params){
        if(FieldCheckUtil.isMapFieldNotNull(params,new String[]{"comList"})){
            List<String>comList=(List<String>)params.get("comList");
            if(comList.size()==0){
                return JsonResult.error("comList不能为空！");
            }
        }
        if(FieldCheckUtil.isMapFieldNotNull(params,new String[]{"buiList"})){
            List<String>buiList=(List<String>)params.get("buiList");
            if(buiList.size()==0){
                return JsonResult.error("buiList不能为空！");
            }
        }
        return JsonResult.success(profitService.getComIdAndBuiProRoomList(params));
    }

}
